﻿#ifndef LOCALUSERMODEL_H
#define LOCALUSERMODEL_H

#include <QQmlEngine>

#include "../global/xnamespace.h"
#include "submodel/VCard.h"

X_BEGIN_NAMESPACE

// 私有数据
class LocalUserModelPrivate : public QSharedData
{
public:
    LocalUserModelPrivate(){ m_vcard = NULL; }
    virtual ~LocalUserModelPrivate() { delete m_vcard; }

public:
    AvailableStatus m_availableStatus;
    QString m_jid;
    QString m_bareJid;
    QString m_password;
    QString m_nickName;
    QString m_photo;
    QString m_thumbnail;
    PhotoType m_photoType;
    QString m_description;

    VCard* m_vcard;
};

// 本地用户模型，作为一个单件在客户端使用.存储的是当前登录用户的信息
class LocalUserModel : public QObject
{
    Q_OBJECT

    Q_PROPERTY(X::AvailableStatus availableStatus READ availableStatus WRITE setAvailableStatus NOTIFY availableStatusChanged)
    Q_PROPERTY(QString jid READ jid WRITE setJid NOTIFY jidChanged)
    Q_PROPERTY(QString bareJid READ bareJid WRITE setBareJid NOTIFY bareJidChanged)
    Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
    Q_PROPERTY(QDate birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
    Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
    Q_PROPERTY(QString telephone READ telephone WRITE setTelephone NOTIFY telephoneChanged)
    Q_PROPERTY(QString mobile READ mobile WRITE setMobile NOTIFY mobileChanged)
    Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
    Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged)
    Q_PROPERTY(QString firstName READ firstName WRITE setFirstName NOTIFY firstNameChanged)
    Q_PROPERTY(QString lastName READ lastName WRITE setLastName NOTIFY lastNameChanged)
    Q_PROPERTY(QString nickName READ nickName WRITE setNickName NOTIFY nickNameChanged)
    Q_PROPERTY(QString photo READ photo WRITE setPhoto NOTIFY photoChanged)
    Q_PROPERTY(QString thumbnail READ thumbnail NOTIFY thumbnailChanged)
    Q_PROPERTY(X::PhotoType photoType READ photoType WRITE setPhotoType NOTIFY photoTypeChanged)
    Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)

signals:
    void availableStatusChanged(X::AvailableStatus availableStatus);
    void bareJidChanged(QString bareJid);
    void passwordChanged(QString password);
    void birthdayChanged(QDate birthday);
    void descriptionChanged(QString description);
    void telephoneChanged(QString telephone);
    void mobileChanged(QString mobile);
    void emailChanged(QString email);
    void addressChanged(QString address);
    void firstNameChanged(QString firstName);
    void lastNameChanged(QString lastName);
    void nickNameChanged(QString nickName);
    void photoChanged(QString photo);
    void thumbnailChanged(QString thumbnail);
    void photoTypeChanged(PhotoType photoType);
    void urlChanged(QString url);
    void jidChanged(QString jid);

public:
    static LocalUserModel &instance();
    static const char *qmlName();
    static QObject *qmlInstance(QQmlEngine* engine, QJSEngine* jsEngine);

public:
    void updateVCard();
    VCard &vcard() const;

public:
    X::AvailableStatus availableStatus() const;
    QDate birthday() const;
    QString bareJid() const;
    QString password() const;
    QString description() const;
    QString telephone() const;
    QString mobile() const;
    QString email() const;
    QString address() const;
    QString firstName() const;
    QString lastName() const;
    QString nickName() const;
    QString photo() const;
    PhotoType photoType() const;
    QString url() const;
    QString jid() const;
    QString thumbnail() const;

public slots:
    LocalUserModel &setAvailableStatus(X::AvailableStatus availableStatus);
    LocalUserModel &setPassword(QString password);
    LocalUserModel &setBirthday(QDate birthday);
    LocalUserModel &setDescription(QString description);
    LocalUserModel &setTelephone(QString telephone);
    LocalUserModel &setMobile(QString mobile);
    LocalUserModel &setEmail(QString email);
    LocalUserModel &setAddress(QString address);
    LocalUserModel &setFirstName(QString firstName);
    LocalUserModel &setLastName(QString lastName);
    LocalUserModel &setNickName(QString nickName);
    LocalUserModel &setPhoto(QString photo);
    LocalUserModel &setPhotoType(PhotoType photoType);
    LocalUserModel &setUrl(QString url);
    LocalUserModel &setBareJid(QString bareJid);
    LocalUserModel &setJid(QString jid);

private:
    LocalUserModel();
    LocalUserModel(const LocalUserModel& src);
    LocalUserModel &operator =(const LocalUserModel& rhs);

    static LocalUserModel *createInstance();

private:
    mutable QSharedDataPointer<LocalUserModelPrivate> d;
};

#define LocalUser LocalUserModel::instance()

X_END_NAMESPACE

#endif // LOCALUSERMODEL_H
